package com.sg.hms.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Room;
import com.sg.hms.Service.RoomService;
import com.sg.hms.ServiceImpl.RoomServiceImpl;
import com.sg.hms.Model.Pagebean;
/***
 * 查看所有房间
 * @author 吴磊
 *
 */
public class ShowAllRoomController extends HttpServlet {
	RoomService show = new RoomServiceImpl();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 获取页码
		req.setCharacterEncoding("utf8");
		String pString = req.getParameter("page");
		// 页码处理页码为空的时候默认为一
		int p = 0;
		if (pString == null) {
			p = 1;
		} else {
			p = Integer.parseInt(pString);
		}
		Pagebean pagebean=new Pagebean(p, 5);
		ArrayList<Room> rooms = show.getRoom();
		int size=rooms.size();
		ArrayList<Room> rooms1=show.getRoom(pagebean);
		resp.setCharacterEncoding("utf8");
		req.setAttribute("page", pagebean.getthisPage(size,p,pagebean.getPageSize()));
		req.setAttribute("rooms", rooms1);
		req.getRequestDispatcher("page/room.jsp").forward(req, resp);

	}

}
